require "classifier_pool"

class PeerRankingService
  @@scores = []
  @@last_recommended_peers = []
  @@rotation_value = 0
  @@ratings = []

  def self.init( pool, initial_seed )
    @@scores = Array.new( pool.num_of_members, 0 )
    @@last_recommended_peers = []
    @@rotation_value = 0
    @@ratings = []

    srand( initial_seed )
  end

  def self.rotation_value
    return @@rotation_value
  end

  def self.set_rotation_value( rotation_value )
    @@rotation_value = rotation_value
  end

  def self.scores
    return @@scores
  end

  def self.ratings
    #
    score_ids = []
    for i in 0..( size_of_scores - 1 )
      score_ids << [ @@scores[ i ], i ]
    end
    sorted_score_ids = score_ids.sort

    #
    id_ratings = []
    rating = 0
    id_ratings << [ sorted_score_ids[ 0 ][ 1 ], rating ]
    for i in 1..( sorted_score_ids.size - 1 )
      if sorted_score_ids[ i - 1 ][ 0 ] != sorted_score_ids[ i ][ 0 ]
        rating = rating + 1
      end
      if sorted_score_ids[ i - 1 ][ 0 ] == sorted_score_ids[ i ][ 0 ]
      end
      id_ratings << [ sorted_score_ids[ i ][ 1 ], rating ]
    end
    sorted_id_ratings = id_ratings.sort
    
    ratings = []
    for i in 0..( sorted_id_ratings.size - 1 )
      ratings << sorted_id_ratings[ i ][ 1 ]
    end

    return ratings
  end

  def self.size_of_scores
    return @@scores.size
  end

  def self.get_recommended_peers( num_of_ensemble_classifier_members, test_case )
    score_id_array = Array.new()
    for i in 0..( size_of_scores - 1 )
      score_id_array << [ @@scores[ i ], i ]
    end
    sorted_score_id_array = score_id_array.sort.reverse

    recommended_peers = []
    if test_case == "query"
      case @@rotation_value % 3
      when 0
        for i in 0..( num_of_ensemble_classifier_members - 1 )
          r = rand( size_of_scores )
          recommended_peers << sorted_score_id_array[ r ][ 1 ]
        end
      else
        for i in 0..( num_of_ensemble_classifier_members - 1 )
          recommended_peers << sorted_score_id_array[ i ][ 1 ]
        end
      end
    elsif test_case == "test"
      for i in 0..( num_of_ensemble_classifier_members - 1 )
        recommended_peers << sorted_score_id_array[ i ][ 1 ]
      end
    end
    recommended_peers.shuffle!
    
    @@last_recommended_peers = recommended_peers

    return recommended_peers
  end

  def self.update_last_recommended_peers( updown )
    @@last_recommended_peers.each { | peer_index |
      case updown
      when '+'
        case @@rotation_value % 3
        when 0
          @@scores[ peer_index ] = @@scores[ peer_index ] + 1
        else
          @@scores[ peer_index ] = @@scores[ peer_index ] + 1
        end
      when '-'
        case @@rotation_value % 3
        when 0
          @@scores[ peer_index ] = @@scores[ peer_index ] - 3
        else
          @@scores[ peer_index ] = @@scores[ peer_index ] - 3
        end
      end
    }
  end

  def self.last_recommended_peers
    return @@last_recommended_peers.sort
  end
end
